package com.white.androidstudy.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.support.v4.widget.TextViewCompat;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by ytf on 2017/11/5 005.
 * Description:
 */

public class ShinnyTextView extends TextView
{


    public ShinnyTextView(Context context)
    {
        super(context);
    }

    public ShinnyTextView(Context context, @Nullable AttributeSet attrs)
    {
        super(context, attrs);
    }

    int width;
    Paint mPaint;
    LinearGradient gradient;
    Matrix mx;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        width = getMeasuredWidth();
        mPaint = getPaint();
        gradient = new LinearGradient(0, 0, width, 0, new int[]{Color.BLUE, 0xFFFFFFFF, Color.BLUE}, null, Shader.TileMode.CLAMP);
        mPaint.setShader(gradient);
        mx = new Matrix();
    }

    int mTranslate;

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        if(mx != null)
        {
            mTranslate += width / 5;
            if(mTranslate > 2* width)
                mTranslate = -width;
            mx.setTranslate(mTranslate, 0);
            gradient.setLocalMatrix(mx);
            postInvalidateDelayed(100);
        }
    }
}
